﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using BusinessObjects.Models;

namespace EFRepository
{
    public class ManagerRepository
    {
        private readonly string dbAddress;
        private readonly string dbPassword;
        private readonly string dbUserId;
        private readonly string initialCatalog;
        private readonly string port;

        public ManagerRepository(string dbAddress, string port, string initialCatalog, string dbUserId,
                                 string dbPassword)
        {
            this.dbAddress = dbAddress;
            this.port = port;
            this.initialCatalog = initialCatalog;
            this.dbUserId = dbUserId;
            this.dbPassword = dbPassword;
            EFContext.SetConnectionString(string.Format(
                "Data Source={0},{1};Network Library=DBMSSOCN;Initial Catalog={2};User ID={3};Password={4}",
                dbAddress, port, initialCatalog, dbUserId, dbPassword));
        }

        #region Users

        public void AddUser(User userInfo)
        {
            using (var dbContext = new EFContext())
            {
                dbContext.Users.Add(userInfo);
                dbContext.SaveChanges();
            }
        }


        public IEnumerable<User> GetAllUsersForDepartment(int departmentId)
        {
            using (var dbContext = new EFContext())
            {
                List<User> users = dbContext.Users.Where(x => x.DepartmentId == departmentId).ToList();
                return users;
            }
        }

        public IEnumerable<Order> GetAllUsersOrders(int departmentId)
        {
            using (var dbContext = new EFContext())
            {
                List<Order> orders =
                    dbContext.Users.Where(x => x.DepartmentId == departmentId).SelectMany(x => x.Orders).ToList();
                return orders;
            }
        }

        public IEnumerable<Dish> GetAllDishesForDepartment(int departmentId)
        {
            using (var dbContext = new EFContext())
            {
                List<Dish> dishes =
                    dbContext.Providers.Where(x => x.DepartmentId == departmentId).SelectMany(x => x.Dishes).ToList();
                return dishes;
            }
        }


        public Dictionary<Dish, int> GetAllUsersDishesFromDate(int departmentId, DateTime date)
        {
            using (var dbContext = new EFContext())
            {
                Dictionary<Dish, int> result =
                    dbContext.Orders.Where(x => x.Date == date && x.User.DepartmentId == departmentId)
                             .SelectMany(x => x.OrderItems).Select(x => x.Dish).GroupBy(x => x.Id)
                             .ToDictionary(gdc => gdc.ToList()[0], gdc => gdc.Count());
                return result;
            }
        }


        public IEnumerable<Order> GetAllUsersOrdersFromDate(int departmentId, DateTime date)
        {
            using (var dbContext = new EFContext())
            {
                List<Order> orders =
                    dbContext.Users.Where(x => x.DepartmentId == departmentId)
                             .SelectMany(x => x.Orders)
                             .Where(x => x.Date == date).ToList();
                return orders;
            }
        }

        public void DeleteUser(User userInfo)
        {
            using (var dbContext = new EFContext())
            {
                dbContext.Users.Remove(userInfo);
                dbContext.SaveChanges();
            }
        }

        public void UpdateUser(User userInfo)
        {
            using (var dbContext = new EFContext())
            {
                User oldUser;
                oldUser = userInfo;
                dbContext.Users.Attach(oldUser);
                oldUser = userInfo;
                dbContext.Entry(oldUser).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        public User GetUser(string login, string password)
        {
            using (var dbContext = new EFContext())
            {
                User res =
                    dbContext.Users.FirstOrDefault(
                        user => user.Login == login && user.Password == password);
                return res;
            }
        }

        #endregion

        #region Dishes

        public void AddDishes(IEnumerable<Dish> dishes, int DepartmentId)
        {
            using (var dbContext = new EFContext())
            {
                foreach (Dish dish in dishes)
                {
                    dbContext.Dishes.Add(dish);
                }
                dbContext.SaveChanges();
            }
        }

        public void RemoveDishes(IEnumerable<Dish> dishes, int DepartmentId)
        {
        }

        public void UpdateDishes(IEnumerable<Dish> dishes, int DepartmentId)
        {
        }

        #endregion
    }
}